﻿
@{
    ViewBag.Title = "模板管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.TemplateName">模板名称：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtKey" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="editTemplate(0);">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/ParaSetting/QueryLabelTemplates'
                    , method: "post"
                    , where: { name: "" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 100 }
                        , { field: 'TemplateName', title: '模板名称', minWidth: 200 }
                        , { field: 'CreateTimeStr', title: '创建时间', minWidth: 150 }
                        , {
                            title: '操作', width: 100, fixed: "right", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-warm" onclick="deleteTemplate(\'' + d.PkId + '\')"><i class="layui-icon">&#xe614;</i>删除</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.name = $("#txtKey").val().trim();

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function editTemplate(templateId) {
            parent.layui.index.openTabsPage("/ParaSetting/EditLabelTemplate?templateId=" + templateId, "模板编辑");
        }

        function deleteTemplate(templateId) {
            layer.confirm('确定要删除模板吗？', { icon: 3, title: '提示' }, function (index) {
                var loadId = layer.load();
                $.ajax({
                    url: "/ParaSetting/RemoveTemplate",
                    data: {
                        templateId: templateId
                    },
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        $("#btnSearch").click();
                    }
                });
                layer.close(index);
            });
        }
    </script>
}
